package mylibrary.network.NetAddress;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Hex;

public class Test {

	public static void main(String[] args) throws Exception {
		
		//getIp("www.163.com");
		//getIp("news.163.com");
		
		//getName("112.84.105.38");
		//getName("112.90.247.175");
		//getName("127.0.0.1");
		
		//getFromURL("http://www.163.com");
		
		//getFromURLConnection("http://www.163.com");
		
		encodeURL("www.163.com/?a=高帅");
	}
	
	public static void getIp(String address) throws Exception {
		
		InetAddress addr = InetAddress.getByName(address);
		System.out.println(address + ":" + addr.getHostAddress());
	}
	
	public static void getName(String ip) throws Exception {
		InetAddress addr = InetAddress.getByName(ip);
		System.out.println(addr.getHostName() + ":" + addr.getHostAddress());
	}
	
	public static void getFromURL(String url) throws Exception {
		
		URL u = new URL(url);
		InputStream in = u.openStream();
		
		OutputStream file = new FileOutputStream("c:\\a.txt");
		
		byte[] b = new byte[1024];
		int c;
		while((c=in.read(b)) != -1) {
			file.write(b);
		}
	}
	
	public static void getFromURLConnection(String url) throws Exception {
		URL u = new URL(url);
		URLConnection uc = u.openConnection();
		
		InputStream in = uc.getInputStream();
		
		OutputStream file = new FileOutputStream("c:\\a.txt");
		
		byte[] b = new byte[1024];
		int c;
		while((c=in.read(b)) != -1) {
			file.write(b);
		} 
	}
	
	public static void encodeURL(String url) throws Exception {
		
		String s = "高帅";
		String s1 = new String(Hex.encodeHex(s.getBytes("utf-8")));
		System.out.println(s1);
		
		//System.out.println(URLEncoder.encode(url));
	}
	
}
